<?php

    function entete($title){
          echo '<!DOCTYPE html>
                <html>
                    <head>
                        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                        <title>'.$title.'</title>
                        <link rel="stylesheet" type="text/css" href="css/style.css">
                        <script language="JavaScript" src="./js/jquery-1.10.2.js"></script>
                        <script language="JavaScript" src="./js/jquery-migrate-1.2.1.min.js"></script>
                    </head>';
    }
    
    function bouton($pValeur, $pNomChampCache, $pAttribut = null) {
        
        if($pAttribut == null) {
            ?>
                <input type="hidden" name="<?php echo $pNomChampCache; ?>" value="true" >
            <?php
        }
        else {
            ?>
                <input type="hidden" name="<?php echo $pNomChampCache; ?>" value="true" <?php echo $pAttribut; ?> >
            <?php
        }?>
            <input type="submit" value="<?php echo $pValeur; ?>" >
        <?php
    }
    
    function getIdParNom($pNom, $pChamp, $pTable) {
        
        // Permet d'obtenir l'id_produit ou l'id_categorie ou l'id_client
        $fin = count($pTable);
        $id = substr($pTable, $fin, -1);
        $id = "id_".$id;
        
        include 'connexion/connexionBDD.php';
        
        $sql = "SELECT ".$id."
                FROM ".$pTable."
                WHERE ".$pChamp." LIKE '".$pNom."'";
        
        $requetePreparee = $pdo->prepare($sql);
        $requeteReussi = $requetePreparee->execute();
        
        if($requeteReussi)
            return false;
       
        $resultat = $requetePreparee->fetch(PDO::FETCH_OBJ);
        
        if($resultat != false)
            return $resultat->$id;
        
        else 
            return false;
        
    }
    
    function getInfosParId($pId, $pTable) {
        
        // Permet d'obtenir l'id_produit ou l'id_categorie ou l'id_client
        $fin = count($pTable);
        $id = substr($pTable, $fin, -1);
        $id = "id_".$id;
        
        include 'connexion/connexionBDD.php';
        
        $sql = "SELECT *
                FROM ".$pTable."
                WHERE ".$id." = ".$pNom."";
        
        $requetePreparee = $pdo->prepare($sql);
        $requeteReussi = $requetePreparee->execute();
        
        if( ! $requeteReussi )
            return false;
        
        else {
            $champ = $requetePreparee->fetch(PDO::FETCH_OBJ);
            return $champ; 
        }    
    }
    
    
    
    // Requete d'insertion
    function insertionBDD($pRequete, $pTableauDeValeur = null) {
        
        include 'connexion/connexionBDD.php';
        
        $requetePreparee = $pdo->prepare($pRequete);
        
        if($pTableauDeValeur == null)
            $requeteReussi = $requetePreparee->execute();
        else
            $requeteReussi = $requetePreparee->execute(array($pTableauDeValeur));
        
        if( ! $requeteReussi )
            return FALSE;
        else 
            return TRUE;
        
    }
    
    // Requete d'affichage d'une seule ligne
    function affichageBDD_uneCol($pRequete) {
        
        include 'connexion/connexionBDD.php';
        
        $requetePreparee = $pdo->prepare($pRequete);
        $requeteReussi = $requetePreparee->execute();
        
        if( ! $requeteReussi )
            return FALSE;
        
        return $requetePreparee->fetch(PDO::FETCH_OBJ);
    }

    // Requete d'affichage de plusieurs lignes
    function affichageBDD_deuxCols($pRequete) {
        
        include 'connexion/connexionBDD.php';
        
        $requetePreparee = $pdo->prepare($pRequete);
        $requeteReussi = $requetePreparee->execute();
        
        if( ! $requeteReussi )
            return FALSE;
        
        $listeInfos = $requetePreparee->fetchAll(PDO::FETCH_OBJ);
        return $listeInfos;
    }
    
        // Requete d'insertion
    function supprLigneBDD($pRequete) {
        
        include 'connexion/connexionBDD.php';
        
        $requetePreparee = $pdo->prepare($pRequete);
        $requeteReussi = $requetePreparee->execute();
        
        if( ! $requeteReussi )
            return FALSE;
        else
            return TRUE;
    }
    
    function comparaisonEntre($pValeurFormulaire, $pValeurBDD, $pChampBDD, $pPremierElement) {
	
	if ($pValeurFormulaire != $pValeurBDD && $pValeurFormulaire != "") {
		
		if($pPremierElement)
			$chaine = " ". $pChampBDD ." = '".$pValeurFormulaire."'";
		else {
			$chaine = ", ". $pChampBDD ." = '".$pValeurFormulaire."'";
		}
		
		return $chaine;
	}
	else {
		return false;
		
	}
    }